package com.atguigu.gmall.msg.listener;

import com.atguigu.gmall.msg.service.SendMsg;
import com.rabbitmq.client.Channel;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class SendMsgListener {
    @Autowired
    SendMsg sendMsg;
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "usm_msg_queue",durable = "true")
            ,exchange =@Exchange(
                    value = "UMS_CODE_EXCHANGE",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC),
        key = "CODE"))
    private void listener(String phone_code, Channel channel, Message message) throws IOException {
        if (StringUtils.isBlank(phone_code)){
            return;
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        //创建索引
        String[] phoneAndCodeArr = StringUtils.split(phone_code, "_");
        sendMsg.proxySendMsg(phoneAndCodeArr[0],phoneAndCodeArr[1]);
    }
}
